package com.example.bysj_sb.dao;

import com.example.bysj_sb.domain.Department;
import com.example.bysj_sb.domain.School;
import com.example.bysj_sb.domain.StudentClass;
import com.example.bysj_sb.service.DegreeService;
import com.example.bysj_sb.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository("studentClassDao")
public class StudentClassDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private DepartmentService departmentService;
    //增加
    public boolean add(StudentClass studentClass) {
        String sql = "INSERT INTO department (no,description,remarks,department_id) VALUES (?,?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, studentClass.getNo(),
                studentClass.getDescription(),studentClass.getRemarks(),studentClass.getDepartment().getId());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
    //通过定义内部类来返回查询结果
    public class StudentClassRowMapper implements RowMapper<StudentClass> {
        @Override
        public StudentClass mapRow(ResultSet resultSet, int i) throws SQLException {
            StudentClass studentClass = new StudentClass();
            studentClass.setId(resultSet.getInt("id"));
            studentClass.setDescription(resultSet.getString("description"));
            studentClass.setNo(resultSet.getString("no"));
            studentClass.setRemarks(resultSet.getString("remarks"));
            Department department =
            departmentService.find(resultSet.getInt("department_id"));
            studentClass.setDepartment(department);
            return studentClass;
        }
    }

    public StudentClass find(int id){
        String sql = "SELECT * FROM StudentClass WHERE id = ?";
        List<StudentClass> studentClasses = jdbcTemplate.query(sql, new Object[]{id}, new StudentClassRowMapper());
        if(!studentClasses.isEmpty()){
            return studentClasses.get(0);
        } else
            return null;
    }

    public List<StudentClass> findAll(){
        String sql = "SELECT * FROM StudentClass ";
        List<StudentClass> studentClasses = jdbcTemplate.query(sql, new StudentClassRowMapper());
        return studentClasses;
    }
    public boolean update(StudentClass studentClass) {
        String sql = "UPDATE StudentClass SET no = ? ,description=? WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql, studentClass.getNo(), studentClass.getDescription(), studentClass.getId());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
    public boolean delete(int id){
        String sql = "DELETE FROM studentClass WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}

